﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using University.Workshop.Model;
using University.Workshop.DataAccess;
using University.Workshop.Business;
using System.Collections.ObjectModel;
using System.Globalization;

namespace University.Workshop.ClientApp
{
    /// <summary>
    /// Lógica de interacción para LiderBoard.xaml
    /// </summary>
    public partial class LiderBoard : Window
    {
        Client client = new Client();
        User user = new User();
        User aux = new User();
        RelationshipsManager relations = new RelationshipsManager();
        ContactsManager contacts = new ContactsManager();
        Collection<User> allHands = new Collection<User>();
        int size, count;
        Profile profile = null;
        public LiderBoard()
        {
            InitializeComponent();
        }

        public LiderBoard(User user)
        {
            InitializeComponent();
            ChargeContacts();
            this.user = user;
            size = relations.All.Count;
            ReadHands();
        }

        public void ReadHands()
        {
            allHands=client.GetAllHands;
            count = allHands.Count;
            for(int i=0;i<count;i++)
            {
                if (lookingFor(allHands[i].Name) == null)
                {
                    allHands.RemoveAt(i);
                    i--;
                    count--;
                }
            }
        }

        public void byLevel()
        {
            for (int i = 0; i < 10 && i < size; i++)
            {
                for (int j = size - 1; j > i; j--)
                {
                    if (relations.All[j].UserLevel > relations.All[j - 1].UserLevel)
                    {
                        aux = relations.All[j];
                        relations.All[j] = relations.All[j - 1];
                        relations.All[j - 1] = aux;
                    }
                }
            }
            Value.Content = "Level:";
            for (int i = 0; i < 10 && i < size; i++)
            {
                listUsers.Items.Add(relations.All[i].Name);
                listValue.Items.Add(relations.All[i].UserLevel + " " + relations.All[i].GetLevel);
            }
        }

        public void byChips()
        {
            for (int i = 0; i < 10 && i < size; i++)
            {
                for (int j = size - 1; j > i; j--)
                {
                    if (relations.All[j].Chips > relations.All[j - 1].Chips)
                    {
                        aux = relations.All[j];
                        relations.All[j] = relations.All[j - 1];
                        relations.All[j - 1] = aux;
                    }
                }
            }
            Value.Content = "Total Chips:";
            for (int i = 0; i < 10 && i < size; i++)
            {
                listUsers.Items.Add(relations.All[i].Name);
                listValue.Items.Add(relations.All[i].Chips);
            }
        }

        public void byGamesWon()
        {
            for (int i = 0; i < 10 && i < size; i++)
            {
                for (int j = size - 1; j > i; j--)
                {
                    if (relations.All[j].TimeWon > relations.All[j - 1].TimeWon)
                    {
                        aux = relations.All[j];
                        relations.All[j] = relations.All[j - 1];
                        relations.All[j - 1] = aux;
                    }
                }
            }
            Value.Content = "Games Won:";
            for (int i = 0; i < 10 && i < size; i++)
            {
                listUsers.Items.Add(relations.All[i].Name);
                listValue.Items.Add(relations.All[i].TimeWon);
            }
        }

        public void byBestHands()
        {
            for (int i = 0; i < 10 && i < count; i++)
            {
                for (int j = count - 1; j > i; j--)
                {
                    if (allHands[j].Chips > allHands[j - 1].Chips)
                    {
                        aux = allHands[j];
                        allHands[j] = allHands[j - 1];
                        allHands[j - 1] = aux;
                    }
                }
            }
            Value.Content = "Best Hands:";
            for (int i = 0; i < 10 && i < count; i++)
            {
                listUsers.Items.Add(allHands[i].Name);
                listValue.Items.Add(allHands[i].Chips);
            }
        }

        private void show_Click(object sender, RoutedEventArgs e)
        {
            ChargeContacts();
            listUsers.Items.Clear();
            listValue.Items.Clear();
            if (level.IsChecked == true)
                byLevel();
            else
            {
                if (total.IsChecked == true)
                    byChips();
                else
                {
                    if (won.IsChecked == true)
                        byGamesWon();
                    else if (hand.IsChecked == true)
                        byBestHands();
                }
            }
        }

        private void listUsers_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (listUsers.SelectedIndex != -1 && listUsers.SelectedValue != null)
            {
                string userSelected = Convert.ToString(listUsers.SelectedItem,CultureInfo.InvariantCulture);
                if (userSelected != null)
                {
                    User userOnly = lookingFor(userSelected);
                   profile = new Profile(user, userOnly, relations);
                    profile.ShowDialog();
                }
                listUsers.SelectedIndex = -1;
                listUsers.SelectedValue = null;
            }
        }

        private void ChargeContacts()
        {
            relations = new RelationshipsManager();
            contacts = new ContactsManager();
            contacts.SetAll(client.GetUsersContacts(user.Name));
            contacts.Separate(user.Name);
            relations.SetAll(client.GetAllUsers);
            Copy(user.Name);
        }
        private User lookingFor(string userName)
        {
            foreach (User userOnly in relations.All)
            {
                if (userOnly.Name == userName)
                    return userOnly;
            }
            return null;
        }
        private void Copy(string userName)
        {
            foreach (UserContacts userContact in contacts.Friends)
            {
                User userOnly;
                if (userContact.UserName == userName)
                    userOnly = lookingFor(userContact.UserRequest);
                else
                    userOnly = lookingFor(userContact.UserName);
                if (userOnly != null)
                {
                    relations.Friends.Add(userOnly);
                }
            }
            foreach (UserContacts userContact in contacts.PendingSend)
            {
                User userOnly = lookingFor(userContact.UserRequest);
                if (userOnly != null)
                {
                    relations.PendingSend.Add(userOnly);
                }
            }
            foreach (UserContacts userContact in contacts.PendingReceive)
            {
                User user = lookingFor(userContact.UserName);
                if (user != null)
                {
                    relations.PendingReceived.Add(user);
                }
            }
            relations.AddPeopleNotKnow(relations.All);

        }

        private void Window_Closed(object sender, EventArgs e)
        {
            if (profile != null)
            {
                if (profile.IsLoaded)
                {
                    profile.Close();
                }
            }
        }
    }
}
